<?php
declare (strict_types = 1);

namespace app\service\logic\common\user;

use think\Model;
use app\model\communal\UserExpertFollowModel;
use app\service\logic\BaseService;

final class ExpertFollowService extends baseservice
{
    protected string $modelClassName = 'UserExpertFollowModel';

    /**
     * 关注/取消
     *
     * @param $params
     * @return bool|Model
     */
    public function create($params)
    {
        $model = UserExpertFollowModel::withSearch(['user_id', 'expert_id'], $params)->findorempty();
        if ($model->isEmpty()) {
            // 关注
            $result = $model->save($params);
        } else {
            // 取消关注
            $result = $model->delete();
        }

        return $result;
    }

    /**
     * 用户是否关注该专家
     *
     * @param $userId
     * @param $expertId
     * @return int
     */
    public function isFollow($userId, $expertId)
    {
        $model = UserExpertFollowModel::where(['user_id' => $userId, 'expert_id' => $expertId])->findOrEmpty();
        return $model->isEmpty() ? UserExpertFollowModel::IS_FOLLOW_TWO : UserExpertFollowModel::IS_FOLLOW_ONE;
    }

    /**
     * 粉丝数量
     *
     * @param $expertId
     * @return int
     */
    public function getFansNumberByExpertId($expertId)
    {
        return UserExpertFollowModel::where('expert_id', $expertId)->count();
    }

    /**
     * 用户已关注专家id
     *
     * @param $userId
     * @return array
     */
    public function getExpertIdsByUserId($userId)
    {
        return UserExpertFollowModel::where('user_id', $userId)->column('expert_id');
    }
}
